package com.gxa.service.impl;

import com.gxa.common.PaginationResult;
import com.gxa.mapper.EmployeeBaseMapper;
import com.gxa.pojo.Employee;
import com.gxa.pojo.Role;
import com.gxa.service.EmployeeBaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: tx
 * Date: 2020/11/02 17:35
 * Description:
 * Version: V1.0
 */
@Service
public class EmployeeBaseServiceImpl implements EmployeeBaseService{

    @Autowired(required = false)
    private EmployeeBaseMapper employeeBaseMapper;


    @Override
    public int addEmployeeBase(Employee employee) {
        int result = employeeBaseMapper.addEmployeeBase(employee);
        Integer employeeId = employee.getEmployeeId();
        for (Role role : employee.getRoleList()) {
            Integer roleId=employeeBaseMapper.selectEmployeeRoleIdByRoleName(role.getRoleName());
            if (roleId!=0){
                employeeBaseMapper.addEmployeeRole(employeeId,roleId);
            }
        }
        return result;
    }

    @Override
    public int delEmployeeBase(Integer employeeId) {
        Employee employee = employeeBaseMapper.queryEmpState(employeeId);
        if(employee.getEmployeeState() == 0){
            return employeeBaseMapper.delEmployeeBase(employeeId);
        }else{
            return 0;
        }
    }

    @Override
    public int updateEmployeeBase(Employee employee) {
        Employee employee1 = employeeBaseMapper.queryEmpState(employee.getEmployeeId());
        if(employee1 != null){
            for (Role role :employee.getRoleList() ) {
                Integer roleId=employeeBaseMapper.selectEmployeeRoleIdByRoleName(role.getRoleName());
                if (roleId!=0){
                    employeeBaseMapper.updateEmployeeRole(employee.getEmployeeId(),roleId);
                }
            }
            return employeeBaseMapper.updateEmployeeBase(employee);
        }
            return 0;
    }

    @Override
    public Employee queryEmployeeBaseByEmployeeId(Employee employee) {
        Employee employee1 = employeeBaseMapper.queryEmployeeBaseByEmployeeId(employee);
        List<Role> roleList=employeeBaseMapper.selectRole(employee1.getEmployeeId());
        employee1.setRoleList(roleList);
        return employee1;
    }

    @Override
    public PaginationResult queryEmployeeBase(int currentPage, int pageSize) {
        PaginationResult paginationResult = new PaginationResult();
        //当前页码数
        paginationResult.setCurrentPage(currentPage);
        //每页显示条数
        paginationResult.setPageSize(pageSize);
        //总条数
        int totalSize = getEmployeeSizeConis();
        //总页数
        int totalPage = totalSize / pageSize;
        if (totalSize % pageSize != 0) {
            totalPage += 1;
        }
        paginationResult.setTotalPage(totalPage);
        paginationResult.setTotalSize(totalSize);
        int param1 = (currentPage - 1) * pageSize;
        int param2 = pageSize;
        List<Employee> employeeList = employeeBaseMapper.queryEmployeeBase(param1, param2);
        for (Employee employee : employeeList) {
            List<Role> roleList=employeeBaseMapper.selectRole(employee.getEmployeeId());
            System.out.println("roleList="+roleList);
            employee.setRoleList(roleList);
            System.out.println("employee="+employee);
        }
        paginationResult.setData(employeeList);
        return paginationResult;
    }


    private int getEmployeeSizeConis() {
        return employeeBaseMapper.getEmployeeSizeConis();
    }
}

